<template>
    <div>
        <el-form :inline="true" :model="selection" class="demo-form-inline">
            <el-form-item label="学生编号">
                <el-input v-model="selection.student_id" placeholder="请输入学号" @change="selectionQuery()"></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="selection.student_name" placeholder="学生姓名" :disabled="true" style="width: 150px;"></el-input>
            </el-form-item>
            <el-form-item style=" margin: 0 100px;">
                <el-radio-group v-model="selection.category" @change="selectionQuery()">
                    <el-radio-button label="1">主修课</el-radio-button>
                    <el-radio-button label="2">选修课</el-radio-button>
                </el-radio-group>
            </el-form-item>
            <el-form-item>
                <el-select v-model="selection.course_name" placeholder="请选择课程">
                    <el-option v-for="c in courseData" :key="c" :label="c" :value="c"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addMinorCourse()">添加选课</el-button>
            </el-form-item>
        </el-form>
        <table style="width: 100%; border: none; margin-top: 30px;" cellspacing="0">
            <tr style="font-weight: 800;">
                <td>课程编号</td>
                <td>课程名称</td>
                <td>成绩</td>
                <td>操作</td>
            </tr>
            <tr v-for="cs in courseAndScoreData" :key="cs.course_id">
                <td>{{ cs.course_id }}</td>
                <td>{{ cs.course_name }}</td>
                <td>{{ cs.final_score }}</td>
                <td>
                    <el-button type="primary" @click="toUpdateScore(cs.course_id)">更新成绩</el-button>
                    <el-button type="danger" @click="delConfirm(cs.course_id)" disabled v-if="selection.category==1">删除</el-button>
                    <el-button type="danger" @click="delConfirm(cs.course_id)" v-if="selection.category==2">删除</el-button>
                </td>
            </tr>
        </table>
    </div>
</template>

<script>
    export default{
        name:"QueryStudentCourse",
        data() {
            return {
               selection:{
                    student_id:"",
                    student_name:"",
                    course_id:"",
                    course_name:"",
                    category:"1",
                    final_score:"",
               },
               courseData:[],
               courseAndScoreData:[],
            }
        },
        created() {
            this.getData();
        },
        methods: {
            getData() {
                this.$axios({
                    methods:'get',
                    url:'http://localhost:8081/workbench/selection/getData',
                }).then(res=>{
                    this.courseData=res.data.data;
                }).catch(err=>{
                    console.log(err);
                })
            },
            selectionQuery() {
                this.$axios({
                    methods:'get',
                    url:'http://localhost:8081/workbench/selection/findSelectionPartByStudentId',
                    params: {
                        student_id:this.selection.student_id,
                        category:this.selection.category,
                    }
                }).then(res=>{
                    this.courseAndScoreData=res.data.data;
                    this.studentNameQuery();
                }).catch(err=>{
                    console.log(err);
                })
            },
            studentNameQuery() {
                this.$axios({
                    methods:'get',
                    url:'http://localhost:8081/workbench/student/findStudentPart',
                    params: {
                        student_id:this.selection.student_id
                    }
                }).then(res=>{
                    this.selection.student_name=res.data.data[0].student_name;
                }).catch(err=>{
                    console.log(err);
                })
            },
            addMinorCourse() {
                this.$axios({
                    method:'post',
                    url:'http://localhost:8081/workbench/selection/addSelectionByStudentId',
                    data: {
                        student_id:this.selection.student_id,
                        course_name:this.selection.course_name,
                    }
                }).then(res=>{
                    this.selectionQuery();
                    console.log(res);
                }).catch(err=>{
                    console.log(err);
                })
            },
            delConfirm(course_id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                this.removeMinorCourse(course_id);
                this.$message({ 
                    type: 'success',
                    message: '删除成功!'
                });
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
                });
            },
            removeMinorCourse(course_id) {
                this.$axios({
                    methods:'get',
                    url:'http://localhost:8081/workbench/selection/removeSelectionByCourseIdAndStudentId',
                    params: {
                        student_id:this.selection.student_id,
                        course_id:course_id,
                    }
                }).then(res=>{
                    this.selectionQuery();
                    console.log(res);
                }).catch(err=>{
                    console.log(err);
                })
            },
            toUpdateScore(course_id) {
                this.$prompt('请输入成绩', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                }).then(({ value }) => {
                    this.updateScore(course_id, value);
                    this.$message({
                        type: 'success',
                        message: '成绩更新为: ' + value,
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消输入'
                    });       
                });
            },
            updateScore(course_id, final_score) {
                
                this.$axios({
                    method:'post',
                    url:'http://localhost:8081/workbench/selection/modifySelectionFinalScore',
                    data: {
                        student_id:this.selection.student_id,
                        course_id:course_id,
                        final_score:final_score
                    }
                }).then(res=>{
                    this.selectionQuery();
                    console.log(res);
                }).catch(err=>{
                    console.log(err);
                })
            }
        }
    }
</script>

<style scoped>
    .el-form-item {
        margin: 5px;
    }
</style>